package znbkListRdtz.presenter;

import android.util.Log;

import znbkListRdtz.api.HeatPointExtensionListApi;
import znbkListRdtz.entity.HeatPointExtensionListEntity;
import znbkListRdtz.IHeatPointExtensionPresenter;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import znbkListRdtz.fragment.HeatPointExtensionListFragment;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.lancoo.rxretrofitlibrary.subscribers.ProgressSubscriber;

import java.util.List;

import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperInfor.ZnbkPaperInfor;
import znbkTestPaperRdtz.activity.HeatPointExtensionPaperActivity;
import znbkTestPaperRdtz.activity.HeatPointExtensionPaperSplActivity;

/**
 * Created by Administrator on 2018\2\5 0005.
 */

public class HeatPointExtensionListPresenter implements IHeatPointExtensionPresenter {
    private ILoadMoreDataView mView;

    private int status = -1;//题型的状态0：未学习，1：已学习
    private String resType = "";//默认所有题型
    private int dateFlag = -1;
    private int indexFlag=1;
    private int pagIndex = 1;
    private int lastPosition = 0;

    private ProgressSubscriber mSubscriber;


    private HttpOnNextListener<HeatPointExtensionListEntity> listener = new HttpOnNextListener<HeatPointExtensionListEntity>() {
        @Override
        public void onNext(HeatPointExtensionListEntity heatPointExtensionListEntity) {
            if (heatPointExtensionListEntity.getTotalCount()> 0) {
                mView.hideLoading();
                List<HeatPointExtensionListEntity.ResourceBean> modelList = heatPointExtensionListEntity.getResource();
                for (int i = 0 ; i < modelList.size() ; i ++){
                    //预处理题库返回不合理的地方
                    String summary = modelList.get(i).getTitle();

                    Log.e("==HttpOnNextListener=0=","ii==="+i+"summary==="+summary);
                   /* summary = TagHelperUtils.replacAllTag(summary,"<P>","");
                    summary = TagHelperUtils.replacAllTag(summary,"<BR>","");
                    summary = TagHelperUtils.replaceNULL_U_Tag(summary);
                    summary = TagHelperUtils.replaceImageTag(summary);
                    if (summary.length() >100 &&
                            !(summary.contains("<table") || summary.contains("<TABLE"))){
                        summary = summary.substring(0,100);
                    }*/

                    String regexStr = "<[^>]*>"; //去除所有的标签
                    summary = summary.replaceAll(regexStr, "");
                    if (summary.length() > 100){
                        summary = summary.substring(0,100);
                    }
                    modelList.get(i).setTitle(summary);
                }

                if (pagIndex == 1) {
                    mView.loadData(heatPointExtensionListEntity);
                    lastPosition = 0;
                }else {
                    mView.loadMoreData(heatPointExtensionListEntity);
                }

            }else {
                mView.showNoData("暂无热点拓展内容");
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mView.showNetError();
        }
    };

    public HeatPointExtensionListPresenter(ILoadMoreDataView view) {
        this.mView = view;
    }

    private void getData(){
        if (mSubscriber != null && !mSubscriber.isUnsubscribed()){
            mSubscriber.unsubscribe();
        }
        HeatPointExtensionListApi api = new HeatPointExtensionListApi(listener,(HeatPointExtensionListFragment)mView);
        api.setResType(resType);
        api.setState(status);
        api.setDateFlag(dateFlag);
        api.setIndexFlag(indexFlag);
        api.setPageNumber(pagIndex);
        api.setPageSize(10+lastPosition);
        mSubscriber = new ProgressSubscriber(api);
        HttpManager.getInstance().doHttpDeal(api,mSubscriber);
    }

    @Override
    public void getData(boolean isShowing) {
        if (isShowing) mView.showLoading();
        pagIndex = 1;
        getData();

    }

    @Override
    public void getMoreData() {
        pagIndex = pagIndex +1;
        getData();
    }

    @Override
    public void getDataByStatus(int quesStatusId) {
        if (status == quesStatusId) return;
        status = quesStatusId;
        HeatPointExtensionListFragment.ResStatusFlag = status;
        getData(false);
    }

    @Override
    public void getDataByQuesType(String quesTypeId) {
        if (resType.equals(quesTypeId)) return;
        if(quesTypeId.equals("-1")){
            quesTypeId = "";
        }
        resType = quesTypeId;
        getData(false);
    }

    @Override
    public void getDataByQuesRank(int quesRankId) {
        if (quesRankId==1){
            dateFlag = -1;
            indexFlag= 1;
        }else if(quesRankId==2){
            dateFlag = 1;
            indexFlag=-1;
        }else if(quesRankId==3){
            dateFlag = 1;
            indexFlag=1;
        }
        getData(false);
    }

    public void openItemDetail(int position, HeatPointExtensionListEntity.ResourceBean  resourceBean) {
        lastPosition = position;
        ZnbkPaperInfor.QuesID = resourceBean.getItemID();
//        //TODO 调用下面方法进行跳转
        if(resourceBean.getResType().equals("4")||
                resourceBean.getResType().equals("6")){
            HeatPointExtensionPaperSplActivity.launch((BaseFragment) mView, resourceBean, ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION_SPL);
        }else {
            HeatPointExtensionPaperActivity.launch((BaseFragment) mView, resourceBean, ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION);
        }
    }
}